package com.jsh.frame.myframe2.utils;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具
 */
public class StringUtil {
    public static boolean isEmpty(String value) {
        return null == value || ("").equals(value.trim()) || ("null").equals(value.toLowerCase());
    }

    public static boolean checkEmail(String email) {
        String str = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    public static boolean checkMobileNO(String value) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(14[0-9])|(17[0-9])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(value);
        return m.matches();
    }

    /**
     * 手机号验证
     *
     * @param str
     * @return 验证通过返回true
     */
    public static boolean isMobile(String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 验证手机号
        m = p.matcher(str);
        b = m.matches();
        return b;
    }

    public static boolean checkPassword(String value) {
        Pattern p = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$");
        Matcher m = p.matcher(value);
        return m.matches();
    }

    public static boolean checkUri(String value) {
        Pattern p = Pattern.compile("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
        Matcher m = p.matcher(value);
        return m.matches();
    }

    public static boolean checkFullName(String value) {
        Pattern p = Pattern.compile("^[\u4e00-\u9fa5]{2,4}");
        Matcher m = p.matcher(value);
        return m.matches();
    }

    public static boolean checkClassName(String value) {
        Pattern p = Pattern.compile("^[a-zA-Z0-9\u4e00-\u9fa5]{1,}");
        Matcher m = p.matcher(value);
        return m.matches();
    }

    public static boolean checkNickName(String value) {
        Pattern p = Pattern.compile("^[a-zA-Z0-9\u4e00-\u9fa5]{2,7}");
        Matcher m = p.matcher(value);
        return m.matches();
    }

    public static boolean checkUserName(String value) {
        Pattern p = Pattern.compile("^(?![0-9]+$)[0-9A-Za-z]{6,16}$");
        Matcher m = p.matcher(value);
        return m.matches();
    }

    /**
     * 生成随机字符串
     *
     * @param length 字符串长度
     * @return
     */
    public static String getRandomString(int length) { //length表示生成字符串的长度
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}